{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## `match`实际上就是从字符串的起始位置开始匹配，只返回第一个匹配结果\n",
    "\n",
    "- \\d == [0-9]\n",
    "- \\D == [^0-9]\n",
    "- \\w == [a-zA-Z0-9_]\n",
    "- \\W == [^a-zA-Z0-9_]\n",
    "- \\d* == \\d\\d\\d\\d\\d\\d\\d\n",
    "\n",
    "## 字符串中搜索`re.search(pattern, string)`也是从字符串开始匹配,只返回第一个匹配结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 0), match=''>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"\\d*\",\"abc1234abcd\") #数字可有可无"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 4), match='1234'>"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"\\d+\",\"1234abcd\") #必须出现数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 1), match='1'>"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"\\d?\",\"1234abcd\") #数字出现0次或者1次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 5), match='1abcd'>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"\\d?[a-z]+\",\"1abcd\") #匹配一个数字以后，后面必须是字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 6), match='12345a'>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"\\d+[a-z]\",\"12345abcd\") #匹配一个数字以后，后面必须是字母"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 4), match='123a'>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"\\d{3}\\w\",\"123adbddaf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 6), match='123adb'>"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"\\d{3}\\w{3}\",\"123adbddaf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 10), match='123adbddaf'>"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"\\d{3}\\w+\",\"123adbddaf\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = re.match(\"1[35678]\\d{9}\",\"18112341234\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'18112341234'"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"a\" ==\"\"+\"a\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 0), match=''>"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"\\d*\",\"a\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 6), match='\\\\nabcd'>"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"\\\\[\\s]?[a-z]*\",\"\\\\nabcd\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 6), match='\\\\nabcd'>"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"\\\\\\\\n\\w*\",\"\\\\nabcd\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 6), match='\\\\nabcd'>"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"\\\\n\\w*\",\"\\\\nabcd\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 5), match='\\\\nabc'>"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"\\\\nabc\",\"\\\\nabcd\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 边界"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- `^` 匹配开始锚定\n",
    "- `$` 匹配结尾锚定\n",
    "- `\\b` 匹配一个单词的边界\n",
    "- `\\B`匹配非单词边界"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 10), match='1812233444'>"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"1[355678]\\d{8}\",\"18122334444\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 11), match='18122334444'>"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"1[355678]\\d{9}$\",\"18122334444\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 11), match='18122334444'>"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"^1[355678]\\d{9}$\",\"18122334444\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 4), match='hove'>"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"^\\w+ve\",\"hover\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [],
   "source": [
    "re.match(r\"^\\w+ve$\",\"hover\") # 匹配不成功"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 5), match='hover'>"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"^\\w+\\b\",\"hover\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 5), match='ho ve'>"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"^\\w+\\s\\bve\\b\",\"ho ve r\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " re.match(r\"^\\w+\\s\\bve\\b\",\"ho ve r\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 5), match='ho ve'>"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " re.match(r\"^.*\\s\\bve\\b\",\"ho ve r\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [],
   "source": [
    " re.match(r\"^.*\\s\\bve\\B\",\"ho ve r\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 5), match='ho ve'>"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " re.match(r\"^.*\\s\\bve\\B\",\"ho ver\") "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 匹配分组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- | 匹配左右任意的的一个表达式\n",
    "- () 将括号里面的的字符作为 一个分组\n",
    "- \\num 引用num数字匹配到的字符\n",
    "- `(?P<name>)` 给分组取别名name\n",
    "- `(?P=name>) `   引用别名为name分组匹配到的字符串\n",
    "\n",
    "- 只匹配【0-100】的整数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 1), match='8'>"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"[1-9]?\\d\",\"8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 2), match='88'>"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"[1-9]?\\d\",\"88\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 2), match='88'>"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"[0]|([1-9]?\\d)\",\"88\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 1), match='0'>"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"[0]|([1-9]?\\d)\",\"0\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 2), match='60'>"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"[0]|([1-9]?\\d)|(100)\",\"60\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 2), match='10'>"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"[0]|(100)|([1-9]?\\d)|\",\"10\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [],
   "source": [
    " re.match(r\"0$|100$|[1-9]\\d$\",\"111\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [],
   "source": [
    " re.match(r\"0$|100$|[1-9]\\d$\",\"00\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    " re.match(r\"0$|100$|[1-9]\\d$\",\"00\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [],
   "source": [
    " re.match(r\"100$|[1-9]?\\d$\",\"00\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 圆括号`()`分组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = re.match(r\"<h1>(.*?)</h1>\",\"<h1>匹配分组</h1>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'匹配分组'"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = re.match(r\"(<h1>).*?(</h1>)\",\"<h1>匹配分组</h1> <h1>Hello world</h1>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<h1>匹配分组</h1>'"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<h1>'"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'</h1>'"
      ]
     },
     "execution_count": 141,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = re.findall(r\"(<h1>).*?(</h1>)\",\"<h1>匹配分组</h1> <h1>Hello world</h1>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('<h1>', '</h1>'), ('<h1>', '</h1>')]"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('<h1>', '</h1>')"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = re.match(r\"(<h1>).*?(</h1>)\",\"<h1>匹配分组</h1>\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<h1>'"
      ]
     },
     "execution_count": 152,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<h1>匹配分组</h1>'"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('<h1>', '</h1>')"
      ]
     },
     "execution_count": 154,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.groups()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'</h1>'"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.groups()[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 266,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = \"<html><h1>a piece of cake</h1></html>\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 37), match='<html><h1>a piece of cake</h1></html>'>"
      ]
     },
     "execution_count": 267,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(\"<.*><.*>.*</.*></.*>\",s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 分组匹配的关键"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 270,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 37), match='<html><h1>a piece of cake</h1></html>'>"
      ]
     },
     "execution_count": 270,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"<(.*)><(.*)>.*</\\2></\\1>\",s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 276,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = r\"(\\w+)@(163|126|gmail|qq)\\.(com|cn|net)$\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = re.match(p,\"maxiaoansuccess@163.com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 23), match='maxiaoansuccess@163.com'>"
      ]
     },
     "execution_count": 278,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 279,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'maxiaoansuccess@163.com'"
      ]
     },
     "execution_count": 279,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 280,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'maxiaoansuccess'"
      ]
     },
     "execution_count": 280,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 281,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'163'"
      ]
     },
     "execution_count": 281,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 282,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'com'"
      ]
     },
     "execution_count": 282,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.group(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 如何给分组取别名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 295,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 37), match='<html><h1>a piece of cake</h1></html>'>"
      ]
     },
     "execution_count": 295,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"<html><h1>a piece of cake</h1></html>\"\n",
    "re.match(r\"<(.*)><(.*)>.*</\\2></\\1>\",s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 具体方法如下，提供别名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 298,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 37), match='<html><h1>a piece of cake</h1></html>'>"
      ]
     },
     "execution_count": 298,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.match(r\"<(?P<key1>.*)><(?P<key2>.*)>.*</(?P=key2)></(?P=key1)>\",s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 299,
   "metadata": {},
   "outputs": [],
   "source": [
    "s1 = \"itcastheima\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 300,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 6), match='itcast'>"
      ]
     },
     "execution_count": 300,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.search(r\"itcast\",s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "metadata": {},
   "outputs": [],
   "source": [
    "re.search(r\"^itcast$\",s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 302,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(0, 6), match='itcast'>"
      ]
     },
     "execution_count": 302,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.search(r\"^itcast\",s1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 308,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<re.Match object; span=(17, 23), match='itcast'>"
      ]
     },
     "execution_count": 308,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2 = \"itchast123213213 itcast\"\n",
    "re.search(r\"itcast\",s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 309,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['itcast']"
      ]
     },
     "execution_count": 309,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.findall(\"itcast\",s2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### sub 将匹配的数据进行替换\n",
    "- 单个替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 312,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = re.sub(r\"\\d+\",\"2020\",\"python = 16666\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 313,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'python = 2020'"
      ]
     },
     "execution_count": 313,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 多个替换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 317,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'python = 16666 cpp python python python'"
      ]
     },
     "execution_count": 317,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = re.sub(r\"php\",\"python\",\"python = 16666 cpp php php php\")\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 324,
   "metadata": {},
   "outputs": [],
   "source": [
    "def replace(result):\n",
    "    print(result.group())\n",
    "    r = int(result.group()) +50\n",
    "    return str(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 325,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16666\n",
      "0\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'python = 16716 cpp php=50 php php'"
      ]
     },
     "execution_count": 325,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = re.sub(r\"\\d+\",replace,\"python = 16666 cpp php=0 php php\")\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 344,
   "metadata": {},
   "outputs": [],
   "source": [
    "html = \"\"\"\n",
    "<p>西邻五子，一子朴，一子敏，一子盲，一子偻，一子跛。乃使朴者农，敏者贾，盲者卜，偻者绩，跛者纺，五子皆不患衣食焉。</p>\n",
    "<p>【译】西边邻居家有五个儿子。一个儿子老实，一个儿子聪明，一个儿子瞎，一个儿子驼背，一个儿子瘸。就让老实的务农，聪明的经商，瞎子卜卦算命，驼背搓麻绳，瘸子纺线，五个儿子都不为衣食发愁。</p>\n",
    "<p>【知识点】因材施教原则</p>\n",
    "<p>✍古文四十七</p>\n",
    "<p>未得乎前，则不敢求其后</p>\n",
    "<p>【译】没有学会或懂得前面的内容，就不能进行后面的内容。</p>\n",
    "<p>【知识点】循序渐进原则</p>\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 349,
   "metadata": {},
   "outputs": [],
   "source": [
    "demo = re.sub(r\"</?\\w+>\",\"\",html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 350,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['',\n",
       " '西邻五子，一子朴，一子敏，一子盲，一子偻，一子跛。乃使朴者农，敏者贾，盲者卜，偻者绩，跛者纺，五子皆不患衣食焉。',\n",
       " '【译】西边邻居家有五个儿子。一个儿子老实，一个儿子聪明，一个儿子瞎，一个儿子驼背，一个儿子瘸。就让老实的务农，聪明的经商，瞎子卜卦算命，驼背搓麻绳，瘸子纺线，五个儿子都不为衣食发愁。',\n",
       " '【知识点】因材施教原则',\n",
       " '✍古文四十七',\n",
       " '未得乎前，则不敢求其后',\n",
       " '【译】没有学会或懂得前面的内容，就不能进行后面的内容。',\n",
       " '【知识点】循序渐进原则',\n",
       " '']"
      ]
     },
     "execution_count": 350,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "re.split(\"\\n\",demo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 百度汉语汉字爬取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "- [马](https://hanyu-word-gif.cdn.bcebos.com/bbbb57f6540734ef3a8de92e3525a6e4e.gif)\n",
      "- [胡](https://hanyu-word-gif.cdn.bcebos.com/b20288e045a4f42b6bd7970181d47c2e7.gif)\n",
      "- [习](https://hanyu-word-gif.cdn.bcebos.com/bfc0c830d893043669b2ffc6b4939def4.gif)\n",
      "- [李](https://hanyu-word-gif.cdn.bcebos.com/b977621f41da34af28cce22a7d9d3fc2b.gif)\n",
      "- [江](https://hanyu-word-gif.cdn.bcebos.com/bf182dbbbd2d54c898aa136074c19b029.gif)\n",
      "- [王](https://hanyu-word-gif.cdn.bcebos.com/b49ec7678427711e5975dc8e0eb15ce01.gif)\n",
      "- [张](https://hanyu-word-gif.cdn.bcebos.com/b1783b747102a489cb6577ba3b5154135.gif)\n",
      "- [马](https://hanyu-word-gif.cdn.bcebos.com/bbbb57f6540734ef3a8de92e3525a6e4e.gif)\n",
      "- [胡](https://hanyu-word-gif.cdn.bcebos.com/b20288e045a4f42b6bd7970181d47c2e7.gif)\n",
      "- [习](https://hanyu-word-gif.cdn.bcebos.com/bfc0c830d893043669b2ffc6b4939def4.gif)\n",
      "- [李](https://hanyu-word-gif.cdn.bcebos.com/b977621f41da34af28cce22a7d9d3fc2b.gif)\n",
      "- [江](https://hanyu-word-gif.cdn.bcebos.com/bf182dbbbd2d54c898aa136074c19b029.gif)\n",
      "- [王](https://hanyu-word-gif.cdn.bcebos.com/b49ec7678427711e5975dc8e0eb15ce01.gif)\n",
      "- [张](https://hanyu-word-gif.cdn.bcebos.com/b1783b747102a489cb6577ba3b5154135.gif)\n",
      "- [马](https://hanyu-word-gif.cdn.bcebos.com/bbbb57f6540734ef3a8de92e3525a6e4e.gif)\n",
      "- [胡](https://hanyu-word-gif.cdn.bcebos.com/b20288e045a4f42b6bd7970181d47c2e7.gif)\n",
      "- [习](https://hanyu-word-gif.cdn.bcebos.com/bfc0c830d893043669b2ffc6b4939def4.gif)\n",
      "- [李](https://hanyu-word-gif.cdn.bcebos.com/b977621f41da34af28cce22a7d9d3fc2b.gif)\n",
      "- [江](https://hanyu-word-gif.cdn.bcebos.com/bf182dbbbd2d54c898aa136074c19b029.gif)\n",
      "- [王](https://hanyu-word-gif.cdn.bcebos.com/b49ec7678427711e5975dc8e0eb15ce01.gif)\n",
      "- [张](https://hanyu-word-gif.cdn.bcebos.com/b1783b747102a489cb6577ba3b5154135.gif)\n",
      "- [马](https://hanyu-word-gif.cdn.bcebos.com/bbbb57f6540734ef3a8de92e3525a6e4e.gif)\n",
      "- [胡](https://hanyu-word-gif.cdn.bcebos.com/b20288e045a4f42b6bd7970181d47c2e7.gif)\n",
      "- [习](https://hanyu-word-gif.cdn.bcebos.com/bfc0c830d893043669b2ffc6b4939def4.gif)\n",
      "- [李](https://hanyu-word-gif.cdn.bcebos.com/b977621f41da34af28cce22a7d9d3fc2b.gif)\n",
      "- [江](https://hanyu-word-gif.cdn.bcebos.com/bf182dbbbd2d54c898aa136074c19b029.gif)\n",
      "- [王](https://hanyu-word-gif.cdn.bcebos.com/b49ec7678427711e5975dc8e0eb15ce01.gif)\n",
      "- [张](https://hanyu-word-gif.cdn.bcebos.com/b1783b747102a489cb6577ba3b5154135.gif)\n",
      "- [马](https://hanyu-word-gif.cdn.bcebos.com/bbbb57f6540734ef3a8de92e3525a6e4e.gif)\n",
      "- [胡](https://hanyu-word-gif.cdn.bcebos.com/b20288e045a4f42b6bd7970181d47c2e7.gif)\n",
      "- [习](https://hanyu-word-gif.cdn.bcebos.com/bfc0c830d893043669b2ffc6b4939def4.gif)\n",
      "- [李](https://hanyu-word-gif.cdn.bcebos.com/b977621f41da34af28cce22a7d9d3fc2b.gif)\n",
      "- [江](https://hanyu-word-gif.cdn.bcebos.com/bf182dbbbd2d54c898aa136074c19b029.gif)\n",
      "- [王](https://hanyu-word-gif.cdn.bcebos.com/b49ec7678427711e5975dc8e0eb15ce01.gif)\n",
      "- [张](https://hanyu-word-gif.cdn.bcebos.com/b1783b747102a489cb6577ba3b5154135.gif)\n"
     ]
    }
   ],
   "source": [
    "import requests        #导入requests包\n",
    "mydit = {}\n",
    "words = [\"习\",\"李\",\"江\",\"王\",\"张\"]\n",
    "for word in words:\n",
    "    url = \"https://hanyu.baidu.com/s?wd=\" + word +\"&ptype=zici\"\n",
    "    strhtml = requests.get(url)        #Get方式获取网页数据\n",
    "    # print(strhtml.text)\n",
    "    result = re.search(r\"http.*\\.gif\",strhtml.text)\n",
    "    result.group()\n",
    "    re2 = re.search(r\"<title>(.)\",strhtml.text)\n",
    "    mydit[re2.group(1)] = result.group() \n",
    "    for k,v in mydit.items():\n",
    "        print(\"- [\" + k + \"]\"+ \"(\" + v +\")\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- [胡](https://hanyu-word-gif.cdn.bcebos.com/b20288e045a4f42b6bd7970181d47c2e7.gif)\n",
    "- [习](https://hanyu-word-gif.cdn.bcebos.com/bfc0c830d893043669b2ffc6b4939def4.gif)\n",
    "- [李](https://hanyu-word-gif.cdn.bcebos.com/b977621f41da34af28cce22a7d9d3fc2b.gif)\n",
    "- [江](https://hanyu-word-gif.cdn.bcebos.com/bf182dbbbd2d54c898aa136074c19b029.gif)\n",
    "- [王](https://hanyu-word-gif.cdn.bcebos.com/b49ec7678427711e5975dc8e0eb15ce01.gif)\n",
    "- [张](https://hanyu-word-gif.cdn.bcebos.com/b1783b747102a489cb6577ba3b5154135.gif)\n",
    "- [马](https://hanyu-word-gif.cdn.bcebos.com/bbbb57f6540734ef3a8de92e3525a6e4e.gif)\n",
    "- [胡](https://hanyu-word-gif.cdn.bcebos.com/b20288e045a4f42b6bd7970181d47c2e7.gif)\n",
    "- [习](https://hanyu-word-gif.cdn.bcebos.com/bfc0c830d893043669b2ffc6b4939def4.gif)\n",
    "- [李](https://hanyu-word-gif.cdn.bcebos.com/b977621f41da34af28cce22a7d9d3fc2b.gif)\n",
    "- [江](https://hanyu-word-gif.cdn.bcebos.com/bf182dbbbd2d54c898aa136074c19b029.gif)\n",
    "- [王](https://hanyu-word-gif.cdn.bcebos.com/b49ec7678427711e5975dc8e0eb15ce01.gif)\n",
    "- [张](https://hanyu-word-gif.cdn.bcebos.com/b1783b747102a489cb6577ba3b5154135.gif)\n",
    "- [马](https://hanyu-word-gif.cdn.bcebos.com/bbbb57f6540734ef3a8de92e3525a6e4e.gif)\n",
    "- [胡](https://hanyu-word-gif.cdn.bcebos.com/b20288e045a4f42b6bd7970181d47c2e7.gif)\n",
    "- [习](https://hanyu-word-gif.cdn.bcebos.com/bfc0c830d893043669b2ffc6b4939def4.gif)\n",
    "- [李](https://hanyu-word-gif.cdn.bcebos.com/b977621f41da34af28cce22a7d9d3fc2b.gif)\n",
    "- [江](https://hanyu-word-gif.cdn.bcebos.com/bf182dbbbd2d54c898aa136074c19b029.gif)\n",
    "- [王](https://hanyu-word-gif.cdn.bcebos.com/b49ec7678427711e5975dc8e0eb15ce01.gif)\n",
    "- [张](https://hanyu-word-gif.cdn.bcebos.com/b1783b747102a489cb6577ba3b5154135.gif)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "str1 = b\"%E8%83%A1\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'%E8%83%A1'"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str1.encode(\"utf-8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
